		/* 	REPLICATION SCRIPT */
		/* Dec 13 2025 */
		

	* ------------------------------------------------------------------------------
	* PART 2: FIGURES
	* Project: School Discipline Disparities Increase when Neighborhood Black Pop Changes
	* Authors: Candipan & Hailey (Science Advances)
	* Dec 2025
	* ------------------------------------------------------------------------------
	* PURPOSE: 
	* This script reproduces all descriptive- and regression-based figures 
	* from the main text (Figures 1-4) and appendix (Figures S1-S5)
	*
	* INPUT DATA:  $data\sa_analysis_final.dta
	* OUTPUT:      $project\figures
	* ------------------------------------------------------------------------------
	* ------------------------------------------------------------------------------
	* INSTRUCTIONS:
	* 1. This runs as a standalone script provided users have pre-defined their global macros. 
	* 2. Ensure all global macros are set.
	* 3. Set Stata to version 17 or higher. Toggle on, if necessary (ln 33)
	* 4. Ensure that reghdfe is version 5 and up and all user-written packages are installed.
	* 5. Run the script.
	* ------------------------------------------------------------------------------

	//clear all
	set more off

	* VERSION REQUIREMENTS:
	
	* Set Stata version 	
	//version 19 // Stata code successfully runs with versions 17-19 (Dec 2025)
 
	
	/********************************************************************	
	 Required user-written commands

	 This do-file uses several community-contributed Stata commands that
	 are NOT part of official Stata:

	   - reghdfe (must use version 5 or 6)
	   - ppmlhdfe
	   - estout (for eststo/esttab)
	   - coefplot

	 Please install these packages before running the code.	
	********************************************************************/
	
	
	*------------------------------------------------------------------------------
		* SAFETY CHECKS: Ensure the Main Master Script has been run, 
		*	subfolders are set, and analysis file is in the data folder: 
		
			** IMPORTANT: Mac/Windows users may need to change the back slashes (\)
			*	to forward slashes (/) in the code below 
	*------------------------------------------------------------------------------
	

	* 1. Check if the global macro to the project root folder is defined 
	if "$project" == "" {
		display as error "Error: The global macro 'project' is not defined."
		display as error "Please run 01_main.do to define your file paths."
		exit 198
	}

	* 2. Check if the figures subfolder has been created
	if "$figures" == "" {
		display as error "Error: You must make sure you have created a figures subfolder for this code to run."
		exit
	}
			
	* 3. Check if the analysis file exists
			capture confirm file "$data/sa_analysis_final.dta"

			if _rc != 0 {
				display as error ///
				"Error: The file 'sa_analysis_final.dta' was not found in the folder: $data"
				display as error ///
				"Please check that you have unzipped the data correctly and that your data folder contains the correct files."
				exit
			}	

	* 4. Confirm which version of reghdfe is installed 
		which reghdfe // version 6.12.3
	
			/*NOTE! 
				The reghdfe syntax works for versions 5 or later. */
				
			
	
	* ------------------------------------------------------------------------------
	* SETUP
	* ------------------------------------------------------------------------------
		
		* Set directory
		cd "$project"
		
		*Import analysis file
		use "$data\sa_analysis_final.dta", clear
		
			sort idschool year 


		*Label variables 
		capture labelvariables
		

			des 
		
	*******************************************************************************
		

		*------------------------------------------------------------------
		* DESCRIPTIVE FIGURES
		*   Figure 1: Overall means and changes
		*   Figure S1: By initially predominantly White (predom)
		*   Figure S2: By locale (igeotype)
		*------------------------------------------------------------------


		*-------------------------------
		* Prep: scale rates to percent
		*-------------------------------
		foreach var in oss_rate_bl oss_rate_wh diff_oss_blwh2 {
			gen `var'_100 = `var' * 100
			gen c_`var'_100 = c_`var' * 100
			local label : variable label `var'
			label var `var'_100 "`label'"
		}


		*--------------------------------------------------------------
		* Figure 1.
		* Summary statistics of discipline rates and disparities
		* by neighborhoods with stable, increasing, decreasing Black pop
		*--------------------------------------------------------------

		* Black, White, and Black–White difference
		foreach oss in oss_rate_bl_100 oss_rate_wh_100 diff_oss_blwh2 {

			local label : variable label `oss'

			* regressions and margins by typeb (0: stable, 1: inc, 2: dec) and year
			forvalues n = 0/2 {

				* by each year
				foreach year in 2010 2018 {
					quietly reg `oss' if typeb == `n' & year == `year' & $conditions
					quietly margins, vsquish noestimcheck post
					eststo m_`n'_`year'
				}

				* change from 2010–2018 (c_ variable)
				quietly reg c_`oss' if typeb == `n' & year == 2018 & $conditions
				quietly margins, vsquish noestimcheck post
				eststo c_`n'
			}

			* create panel with Stable / Increasing / Decreasing
			coefplot (m_0_2010, label(2010) ///
					bcolor(black*.6) ciopts(lcol(black))) ///
					(m_0_2018, label(2018) ///
					bcolor(black*.2) ciopts(lcol(black))) ///
					(c_0, label(Change from 2010 to 2018) ///
					fcolor(white) lcolor(black) ciopts(lcol(black))) , ///
					bylabel("Stable") ///
					|| ///
					m_1_2010 m_1_2018 c_1, bylabel("Increasing") ///
					|| ///
					m_2_2010 m_2_2018 c_2, bylabel("Decreasing") ///
					|| , ///
					vert barwidth(0.25) ytitle("") recast(bar) ///
					ysc(r(-10 20) off) ///
					ylabel(none) ///
					xlabel(1 " " , noticks) ///
					yline(0) ///
					xtitle("") ///
					color(*.5) $ciopts ///
					mlabel(string(@b, "%3.1f")) ///
					mlabpos(5) mlabsize(medium) mlabcolor(black) ///
					byopts(row(1) title("`label'", size(medium)) ///
					legend(off) compact) ///
					subtitle(, fcolor(none) lstyle(none) size(medium)) ///
					name(fig_desc_main_`oss', replace)

		}

		* combine three panels for Figure 1
		graph combine ///
				fig_desc_main_oss_rate_bl_100 ///
				fig_desc_main_oss_rate_wh_100 ///
				fig_desc_main_diff_oss_blwh2, ///
				row(3) imargin(zero) ///
				name(fig1_desc_main_combo, replace) 
			
			* Save Figure 1
			graph export "$figures\fig1_desc_main_combo.tif", as(tif) /*name("fig_desc_main_combo")*/ replace

			graph drop fig_desc_main_oss*  fig_desc_main_diff*

		*--------------------------------------------------------------
		* Figure S1.
		* Summary stats by initially predominantly White (predom)
		*--------------------------------------------------------------

		foreach pred in 0 1 {

			local label_predom : label (predom) `pred'
			foreach oss in oss_rate_bl_100 oss_rate_wh_100 diff_oss_blwh2 {
				local label : variable label `oss'
				forvalues n = 0/2 {

					* by year within predom group
					foreach year in 2010 2018 {
						quietly reg `oss' if typeb == `n' & year == `year' ///
							& predom == `pred' & $conditions
						quietly margins, vsquish noestimcheck post
						eststo m_`n'_`year'
					}

					* change from 2010–2018
					quietly reg c_`oss' if typeb == `n' & year == 2018 ///
						& predom == `pred' & $conditions
					quietly margins, vsquish noestimcheck post
					eststo c_`n'
				}

				coefplot (m_0_2010 , label(2010) ///
							bcolor(black*.6) ciopts(lcol(black))) ///
						 (m_0_2018 , label(2018) ///
							bcolor(black*.2) ciopts(lcol(black))) ///
						 (c_0 , label(Change from 2010 to 2018) ///
							fcolor(white) lcolor(black) ciopts(lcol(black))) ///
						 , bylabel("Stable") ///
						 || ///
						 m_1_2010 m_1_2018 c_1, bylabel("Increasing") ///
						 || ///
						 m_2_2010 m_2_2018 c_2, bylabel("Decreasing") ///
						 || , ///
						 vert barwidth(0.2) ytitle("") recast(bar) ///
						 ysc(r(-10 25) off) ///
						 ylabel(-10(10)20, labsize(medium)) ///
						 xlabel(1 " " , noticks) ///
						 yline(0) ///
						 xtitle("") ylabel(, labsize(small)) ///
						 color(*.5) $ciopts ///
						 mlabel(string(@b, "%3.1f")) ///
						 mlabpos(5) mlabsize(medium) mlabcolor(black) ///
						 byopts(row(1) title("`label'", size(medium)) ///
								legend(off) compact) ///
						 subtitle(, fcolor(none) lstyle(none) size(medium)) ///
						 name(fig_desc_predom`pred'_`oss', replace)

			}

			* combine the three outcomes within this predom group
			graph combine fig_desc_predom`pred'_oss_rate_bl_100 ///
						  fig_desc_predom`pred'_oss_rate_wh_100 ///
						  fig_desc_predom`pred'_diff_oss_blwh2, ///
				row(3) ///
				title("`label_predom'", size(medsmall)) ///
				imargin(zero) ///
				name(fig_desc_predom`pred'_combo, replace)
		}


			
		* combine not-predom and predom White panels
		graph combine ///
				fig_desc_predom0_combo ///
				fig_desc_predom1_combo, ///
				col(2) ///
				name(fig_s1_desc_predom_all, replace) ///
				note(left bar is 2010; center is 2018; right is change from 2010-18)
			
		*Save Figure S1
		graph export ///
			"$figures\fig_s1_desc_predom_all.tif", ///
			as(tif) /*name("fig_s1_desc_predom_all")*/ replace
		

			graph drop fig_desc_predom*combo
			graph drop fig_desc_predom*diff_oss_blwh2 fig_desc_predom*oss*100

			
		*--------------------------------------------------------------
		* Figure S2.
		* Summary stats by locale (igeotype: 0,1,2)
		*--------------------------------------------------------------

		foreach geo in 0 1 2 {
			local label_igeotype : label (igeotype) `geo'
			foreach oss in oss_rate_bl_100 oss_rate_wh_100 diff_oss_blwh2 {
				local label : variable label `oss'
				forvalues n = 0/2 {

					* by year within locale
					foreach year in 2010 2018 {
						quietly reg `oss' if typeb == `n' & year == `year' ///
							& igeotype == `geo' & $conditions
						quietly margins, vsquish noestimcheck post
						eststo m_`n'_`year'
					}

					* change from 2010–2018
					quietly reg c_`oss' if typeb == `n' & year == 2018 ///
						& igeotype == `geo' & $conditions
					quietly margins, vsquish noestimcheck post
					eststo c_`n'
				}

			coefplot (m_0_2010 , label(2010) ///
					bcolor(black*.6) ciopts(lcol(black))) ///
					(m_0_2018 , label(2018) ///
					bcolor(black*.2) ciopts(lcol(black))) ///
					(c_0, label(Change from 2010 to 2018) ///
					fcolor(white) lcolor(black) ciopts(lcol(black))) ///
					, bylabel("Stable") ///
					|| m_1_2010  m_1_2018  c_1, bylabel("Increasing") ///
					|| m_2_2010  m_2_2018  c_2, bylabel("Decreasing") ///
					|| , vert barwidth(0.25) ytitle("") recast(bar) ///
					ysc(r(-10 25) off) ///
					ylabel(-10(10)20, labsize(medium)) ///
					xlabel(1 " " , noticks) ///
					yline(0) ///
					xtitle("") ylabel(, labsize(small)) ///
					color(*.5) $ciopts ///
					mlabel(string(@b, "%3.1f")) ///
					mlabpos(12) mlabsize(medium) mlabcolor(black) ///
					byopts(row(1) title("`label'", size(medium)) ///
					legend(off) compact) ///
					subtitle(, fcolor(none) lstyle(none) size(medium)) ///
					name(fig_desc_geo`geo'_`oss', replace)

    }

		* combine three outcomes within this locale
		graph combine ///
				fig_desc_geo`geo'_oss_rate_bl_100 ///
				fig_desc_geo`geo'_oss_rate_wh_100 ///
				fig_desc_geo`geo'_diff_oss_blwh2, ///
				row(3) ///
				title("`label_igeotype'", size(medsmall)) ///
				imargin(zero) ///
				name(fig_desc_geo`geo'_combo, replace)

	}

		* combine urban, suburban, rural
		graph combine ///
				fig_desc_geo0_combo ///
				fig_desc_geo1_combo ///
				fig_desc_geo2_combo, ///
				col(3) ///
				name(fig_s2_desc_locale_all, replace) ///
				note(left bar is 2010; center is 2018; right is change from 2010-18)
				
		* Save Figure S2	
		graph export "$figures\fig_s2_desc_locale_all.tif", ///
				as(tif) /*name("fig_s2_desc_locale_all")*/ replace
			
				graph drop fig_desc_geo*
			
		*------------------------------------------------------------------
		* REGRESSION-BASED FIGURES (RATE DIFFERENCE AND IRR)
		*   Uses: diff_oss_blwh2, irr_oss_blwh2
		*   Key grouping variables: typeb, predom, igeotype
		*------------------------------------------------------------------


		*--------------------------------------------------------------
		* Figure 2. Average marginal effect on Black–White OSS difference,
		*           by neighborhood Black-population change (typeb)
		*--------------------------------------------------------------
				
		estimates clear

		eststo main: ///
		quietly reghdfe diff_oss_blwh2 ///
			$neighvars ///
			$schvars ///
			i.year##i.typeb ///
			if $conditions, ///
			$regopts

		local i 1
		forvalues n = 0/2 {
			est restore main
			quietly margins if typeb == `n', ///
				dydx(year) vsquish noestimcheck post
			eststo m`i'
			local ++i
		}

		coefplot (m1 , label(Stable)     bcolor(navy)  ciopts(lcol(black))) ///
				 (m2 , label(Increasing) bcolor(green) ciopts(lcol(black))) ///
				 (m3 , label(Decreasing) bcolor(gray)  ciopts(lcol(black))) , ///
				 vert barwidth(0.12) ytitle("") recast(bar) ///
				 xlabel(1 " " , noticks) ///
				 ysc(r(-2 6) off) ///
				 ylabel(-2(2)4, labsize(small)) yline(0) ///
				 xtitle("") ylabel(, labsize(small)) ///
				 color(*.5) $ciopts ///
				 $figopts mlabpos(2) mlabsize(small) mlabcolor(black) ///
				 name(fig2_ossdiff_baseline_neightype, replace) ///
				 note("Figure 2. Average marginal effect on Black–White OSS difference from 2010-18 by neighborhood type", size(small))


			* Save Figure 2
			graph export "$figures\fig2_ossdiff_baseline_neightype.tif", as(tif) replace
			 

		*--------------------------------------------------------------
		* Figure 3. Average marginal effect on OSS difference,
		*           by initial predominantly White (predom)
		*--------------------------------------------------------------

			local label : variable label predom

			* not predominantly White (p = 0)
			foreach p in 0 1 {
				local v : label (predom) `p'
				estimates clear
				eststo predom_`p' : quietly reghdfe diff_oss_blwh2 ///
					$neighvars ///
					$schvars ///
					i.year##i.typeb ///
					if $conditions & predom == `p', ///
					$regopts

				local i 1
				forvalues n = 0/2 {
					est restore predom_`p'
					quietly margins if typeb == `n', ///
						dydx(year) noestimcheck post
					eststo m`i'
					local ++i
				}

				coefplot (m1 , label(Stable)     bcolor(navy)  ciopts(lcol(black))) ///
						 (m2 , label(Increasing) bcolor(green) ciopts(lcol(black))) ///
						 (m3 , label(Decreasing) bcolor(gray)  ciopts(lcol(black))) , ///
						 vert barwidth(0.12) ytitle("") recast(bar) ///
						 title("`v'", size(medium)) ///
						 xlabel(1 " " , noticks) ///
						 ysc(r(-6 8) off) ///
						 ylabel(-6(2)8, labsize(small)) yline(0) ///
						 xtitle("") ylabel(, labsize(small)) ///
						 color(*.5) $ciopts ///
						 $figopts mlabpos(4) mlabsize(medium) mlabcolor(black) ///
						 nokey ///
						 name(fig_rate_predom_`p', replace)

			}

			* combine non-predom and predom White panels
			graph combine fig_rate_predom_0 fig_rate_predom_1, ///
				ycommon rows(1) imargin(2 2 0 0) ///
				name(fig3_ossdiff_predom_combo, replace) ///
				note("Figure 3. Average marginal effect on Black–White OSS difference from 2010-18 by initial neighborhood pct White", size(small))


			* Save Figure 3
			graph export "$figures\fig3_ossdiff_predom_combo.tif", as(tif) replace
			 
			graph drop fig_rate_predom*

			
		*--------------------------------------------------------------
		* Figure 4. Average marginal effect on OSS difference,
		*           by locale (igeotype: 0 urban, 1 suburban, 2 rural)
		*--------------------------------------------------------------

		foreach p in 0 1 2 {
			local v : label (igeotype) `p'
			estimates clear
			eststo igeotype_`p' : quietly reghdfe diff_oss_blwh2 ///
				$neighvars ///
				$schvars ///
				i.year##i.typeb ///
				if $conditions & igeotype == `p', ///
				$regopts

			local i 1
			forvalues n = 0/2 {
				est restore igeotype_`p'
				quietly margins if typeb == `n', ///
					dydx(year) noestimcheck post
				eststo m`i'
				local ++i
			}

			coefplot (m1 , label(Stable)     bcolor(navy)  ciopts(lcol(black))) ///
					 (m2 , label(Increasing) bcolor(green) ciopts(lcol(black))) ///
					 (m3 , label(Decreasing) bcolor(gray)  ciopts(lcol(black))) , ///
					 vert barwidth(0.12) ytitle("") recast(bar) ///
					 title("`v'", size(medium)) ///
					 xlabel(1 " " , noticks) ///
					 ysc(r(-6 10) off) ///
					 ylabel(-6(2)8, labsize(small)) yline(0) ///
					 xtitle("") ylabel(, labsize(small)) ///
					 color(*.5) $ciopts ///
					 $figopts mlabpos(7) mlabsize(medium) mlabcolor(black) ///
					 nokey ///
					 name(fig_rate_igeotype`p', replace)

		}

		graph combine fig_rate_igeotype0 fig_rate_igeotype1 fig_rate_igeotype2, ///
			ycommon rows(1) imargin(2 2 0 0) ///
			name(fig4_ossdiff_locale_combo, replace) ///
			note("Figure 4. Average marginal effect on Black–White OSS difference from 2010-18 by locale", size(small))

			
			* Save Figure 4
			graph export "$figures\fig4_ossdiff_locale_combo.tif", as(tif) replace
			
			graph drop fig_rate_igeotype*

		*--------------------------------------------------------------
		* Figure S3. IRR of OSS difference (ppmlhdfe),
		*            all neighborhoods and by predom/locale
		*--------------------------------------------------------------

		* All neighborhoods
		estimates clear
		foreach typeb in 0 1 2 {
			capture drop m1
			quietly ///
			ppmlhdfe irr_oss_blwh2 ///
				$neighvars ///
				$schvars ///
				i.year##(ib`typeb'.typeb) ///
				if $conditions, ///
				$regopts nocons ///
				d(m1) eform 

			eststo typeb_`typeb'
		}

		coefplot (typeb_0 , label(Stable)     bcolor(navy)  ciopts(lcol(black))) ///
				 (typeb_1 , label(Increasing) bcolor(green) ciopts(lcol(black))) ///
				 (typeb_2 , label(Decreasing) bcolor(gray)  ciopts(lcol(black))) , ///
				 keep(2018.year) eform ///
				 vert barwidth(0.2) ytitle("") recast(bar) ///
				 xlabel(1 " " , noticks) yline(1) ///
				 xtitle("") ylabel(, labsize(small)) ///
				 color(*.5) $ciopts ///
				 $figopts mlabpos(10) mlabsize(medium) mlabcolor(black) ///
				 nokey ///
				 title("All Neighborhoods", size(medium)) ///
				 name(fig_s3_irr_main_typeb, replace) ///
					note("Figure S3: IRR Main")

			
		* By predom White
		foreach var in predom {
			local label : variable label `var'
			foreach p in 0 1 {
				local v : label (`var') `p'
				estimates clear

				foreach typeb in 0 1 2 {
					capture drop m1
					quietly ///
					ppmlhdfe irr_oss_blwh2 ///
						$neighvars ///
						$schvars ///
						i.year##(ib`typeb'.typeb) ///
						if $conditions & `var' == `p', ///
						$regopts nocons ///
						d(m1) eform

					eststo typeb_`typeb'
				}

			coefplot (typeb_0 , label(Stable)     bcolor(navy)  ciopts(lcol(black))) ///
					 (typeb_1 , label(Increasing) bcolor(green) ciopts(lcol(black))) ///
					 (typeb_2 , label(Decreasing) bcolor(gray)  ciopts(lcol(black))) , ///
					 keep(2018.year) eform ///
					 vert barwidth(0.2) ytitle("") recast(bar) ///
					 title("`v'", size(medium)) ///
					 xlabel(1 " " , noticks) yline(1) ///
					 xtitle("") ylabel(, labsize(small)) ///
					 color(*.5) $ciopts $figopts ///
					 mlabpos(10) mlabsize(medium) mlabcolor(black) ///
					 nokey ///
					 name(fig_irr_`var'_`p', replace)

			}

			graph combine fig_irr_`var'_0 fig_irr_`var'_1, ///
				ycommon rows(1) imargin(2 2 0 0) ///
				title("by `label'", size(medium)) ///
				name(fig_s3_irr_`var'_combo, replace) ///
					note("Figure S3: IRR Predominantly White vs Nonwhite")
		}


		* By locale (igeotype)
		foreach var in igeotype {
			local label : variable label `var'
			foreach p in 0 1 2 {
				local v : label (`var') `p'
				estimates clear

				foreach typeb in 0 1 2 {
					capture drop m1
					quietly ///
					ppmlhdfe irr_oss_blwh2 ///
						$neighvars ///
						$schvars ///
						i.year##(ib`typeb'.typeb) ///
						if $conditions & `var' == `p', ///
						$regopts nocons ///
						d(m1) eform 

					eststo typeb_`typeb'
				}

			coefplot (typeb_0 , label(Stable)     bcolor(navy)  ciopts(lcol(black))) ///
					 (typeb_1 , label(Increasing) bcolor(green) ciopts(lcol(black))) ///
					 (typeb_2 , label(Decreasing) bcolor(gray)  ciopts(lcol(black))) , ///
					 keep(2018.year) eform ///
					 vert barwidth(0.2) ytitle("") recast(bar) ///
					 title("`v'", size(medium)) ///
					 xlabel(1 " " , noticks) yline(1) ///
					 xtitle("") ylabel(, labsize(small)) ///
					 color(*.5) $ciopts $figopts ///
					 mlabpos(10) mlabsize(medium) mlabcolor(black) ///
					 nokey ///
					 name(fig_irr_`var'_`p', replace)
			}

			graph combine fig_irr_`var'_0 fig_irr_`var'_1 fig_irr_`var'_2, ///
				ycommon rows(1) imargin(2 2 0 0) ///
				title("by locale", size(medium)) ///
				name(fig_s3_irr_`var'_combo, replace) ///
					note("Figure S3: IRR Locale")

		}

				graph drop fig_irr*


			* Save Appendix Figures S3
			
				// Main
			graph export 	"$figures\fig_s3_irr_main_typeb.tif", ///
							name("fig_s3_irr_main_typeb") as(tif) replace
							
				// Predominantly White
			graph export 	"$figures\fig_s3_irr_predom_combo.tif", ///
							name("fig_s3_irr_predom_combo") as(tif) replace
							
				// Locale			
			graph export 	"$figures\fig_s3_irr_igeotype_combo.tif", ///
							name("fig_s3_irr_igeotype_combo") as(tif) replace
			
			
		*--------------------------------------------------------------
		* Figure S4. Marginal effects, restricted to schools with
		*            at least 20 Black and 20 White students
		*--------------------------------------------------------------

		foreach max in 20 {

			* all neighborhoods

			eststo main_max: quietly reghdfe diff_oss_blwh2 ///
				$neighvars ///
				$schvars ///
				i.year##i.typeb ///
				if $conditions & $max , ///
				$regopts

			local i 1
			forvalues n = 0/2 {
				est restore main_max
				quietly margins if typeb == `n', ///
					dydx(year) vsquish noestimcheck post
				eststo m`i'
				local ++i
			}

			coefplot (m1 , label(Stable)     bcolor(navy)  ciopts(lcol(black))) ///
				 (m2 , label(Increasing) bcolor(green) ciopts(lcol(black))) ///
				 (m3 , label(Decreasing) bcolor(gray)  ciopts(lcol(black))) , ///
				 vert barwidth(0.2) ytitle("") recast(bar) ///
				 title("All Neighborhoods", size(medium)) ///
				 xlabel(1 " " , noticks) ///
				 ysc(r(-10 10) off) ///
				 ylabel(-10(5)10, labsize(small)) yline(0) ///
				 xtitle("") ylabel(, labsize(small)) ///
				 color(*.5) $ciopts $figopts ///
				 mlabpos(10) mlabsize(medium) mlabcolor(black) ///
				 name(fig_s4_rate_max`max'_main, replace) ///
				note("Figure S4: Main")


			* by predom White
			foreach var in predom {

				foreach p in 0 1 {

				local v : label (`var') `p'

				eststo `var'_`p'_max: quietly reghdfe diff_oss_blwh2 ///
					$neighvars ///
					$schvars ///
					i.year##i.typeb ///
					if $conditions & $max & `var'==`p' , ///
					$regopts

				local i 1
				forvalues n = 0/2 {
					est restore `var'_`p'_max
					quietly margins if typeb == `n', ///
						dydx(year) noestimcheck post
					eststo m`i'
					local ++i
				}

				coefplot (m1 , label(Stable)     bcolor(navy)  ciopts(lcol(black))) ///
					 (m2 , label(Increasing) bcolor(green) ciopts(lcol(black))) ///
					 (m3 , label(Decreasing) bcolor(gray)  ciopts(lcol(black))) , ///
					 vert barwidth(0.2) ytitle("") recast(bar) ///
					 title("`v'", size(medium)) ///
					 xlabel(1 " " , noticks) ///
					 ysc(r(-10 10) off) ///
					 ylabel(-10(5)10, labsize(small)) yline(0) ///
					 xtitle("") ylabel(, labsize(small)) ///
					 color(*.5) $ciopts $figopts ///
					 mlabpos(10) mlabsize(medium) mlabcolor(black) ///
					 nokey ///
					 name(fig_rate_max`max'_`var'_`p', replace)

				}

				graph combine fig_rate_max`max'_`var'_0 fig_rate_max`max'_`var'_1, ///
					ycommon rows(1) imargin(2 2 0 0) ///
					title("by Initially Predominantly White", size(medium)) ///
					name(fig_s4_rate_max`max'_`var'_combo, replace) ///
					note("Figure S4: Predominantly White vs Nonwhite")

			}


			* by locale
			foreach p in 0 1 2 {

				local v : label (igeotype) `p'
	
				eststo igeotype_`p'_max: quietly reghdfe diff_oss_blwh2 ///
					$neighvars ///
					$schvars ///
					i.year##i.typeb ///
					if $conditions & $max & igeotype==`p' , ///
					$regopts

				local i 1
				forvalues n = 0/2 {
					est restore igeotype_`p'_max
					quietly margins if typeb == `n', ///
						dydx(year) noestimcheck post
					eststo m`i'
					local ++i
				}

			coefplot (m1 , label(Stable)     bcolor(navy)  ciopts(lcol(black))) ///
					 (m2 , label(Increasing) bcolor(green) ciopts(lcol(black))) ///
					 (m3 , label(Decreasing) bcolor(gray)  ciopts(lcol(black))) , ///
					 vert barwidth(0.2) ytitle("") recast(bar) ///
					 title("`v'", size(medium)) ///
					 xlabel(1 " " , noticks) ///
					 ysc(r(-10 10) off) ///
					 ylabel(-10(5)10, labsize(small)) yline(0) ///
					 xtitle("") ylabel(, labsize(small)) ///
					 color(*.5) $ciopts $figopts ///
					 mlabpos(10) mlabsize(medium) mlabcolor(black) ///
					 nokey ///
					 name(fig_rate_max`max'_igeotype`p', replace)

			}

			graph combine fig_rate_max`max'_igeotype0 ///
						  fig_rate_max`max'_igeotype1 ///
						  fig_rate_max`max'_igeotype2, ///
				ycommon rows(1) imargin(2 2 0 0) ///
				title("by Locale", size(medium)) ///
				name(fig_s4_rate_max`max'_locale_combo, replace) ///
					note("Figure S4: Locale")

		}

				graph drop fig_rate_max*
				
				
			* Save Appendix Figures S4
			
				// Main
			graph export "$figures\fig_s4_rate_max20_main.tif", ///
				name("fig_s4_rate_max20_main") as(tif) replace	
			
				// Predominantly White
			graph export "$figures\fig_s4_rate_max20_predom_combo.tif", ///
				name("fig_s4_rate_max20_predom_combo") as(tif) replace	
			
				// Locale
			graph export "$figures\fig_s4_rate_max20_locale_combo.tif", ///
				name("fig_s4_rate_max20_locale_combo") as(tif) replace	
			

		*--------------------------------------------------------------
		* Figure S5. Marginal effects by 10 percent change in % Black
		*--------------------------------------------------------------

		* create indicators for neighborhoods with 10% increase/decrease in pct neigh Black  
			capture drop black_10 
			capture drop ngh_black_10
			gen black_10 = 0 if year == 2018
			replace black_10 = 1 if sgr_pnhb >= .10  & year == 2018
			replace black_10 = 2 if sgr_pnhb <= -.10 & year == 2018
			bysort idschool: egen ngh_black_10 = max(black_10)
				

		* All neighborhoods
		estimates clear

		eststo main_10: quietly reghdfe diff_oss_blwh2 ///
			$neighvars ///
			$schvars ///
			i.year##i.ngh_black_10 ///
			if $conditions, ///
			$regopts

		local i 1
		forvalues n = 0/2 {
			est restore main_10
			quietly margins if ngh_black_10 == `n', ///
				dydx(year) vsquish noestimcheck post
			eststo m`i'
			local ++i
		}

		coefplot (m1 , label(Stable)     bcolor(navy)  ciopts(lcol(black))) ///
				 (m2 , label(Increasing) bcolor(green) ciopts(lcol(black))) ///
				 (m3 , label(Decreasing) bcolor(gray)  ciopts(lcol(black))) , ///
				 vert barwidth(0.2) ytitle("") recast(bar) ///
				 title("All Neighborhoods", size(medium)) ///
				 xlabel(1 " " , noticks) ///
				 ysc(r(-10 10) off) ///
				 ylabel(-10(5)10, labsize(small)) yline(0) ///
				 xtitle("") ylabel(, labsize(small)) ///
				 color(*.5) $ciopts $figopts ///
				 mlabpos(10) mlabsize(medium) mlabcolor(black) ///
				 name(fig_s5_rate_10_main, replace) ///
					note("Figure S5: Main")


		* By predom White
		foreach var in predom {

			foreach p in 0 1 {

				local v : label (`var') `p'
				estimates clear

				eststo `var'_`p'_10: quietly reghdfe diff_oss_blwh2 ///
					$neighvars ///
					$schvars ///
					i.year##i.ngh_black_10 ///
					if $conditions & `var' == `p', ///
					$regopts

				local i 1
				forvalues n = 0/2 {
					est restore `var'_`p'_10
					quietly margins if ngh_black_10 == `n', ///
						dydx(year) noestimcheck post
					eststo m`i'
					local ++i
				}

			coefplot (m1 , label(Stable)     bcolor(navy)  ciopts(lcol(black))) ///
					 (m2 , label(Increasing) bcolor(green) ciopts(lcol(black))) ///
					 (m3 , label(Decreasing) bcolor(gray)  ciopts(lcol(black))) , ///
					 vert barwidth(0.2) ytitle("") recast(bar) ///
					 title("`v'", size(medium)) ///
					 xlabel(1 " " , noticks) ///
					 ysc(r(-10 10) off) ///
					 ylabel(-10(5)10, labsize(small)) yline(0) ///
					 xtitle("") ylabel(, labsize(small)) ///
					 color(*.5) $ciopts $figopts ///
					 mlabpos(10) mlabsize(medium) mlabcolor(black) ///
					 nokey ///
					 name(fig_rate_10_`var'_`p', replace)

			}

			graph combine fig_rate_10_`var'_0 fig_rate_10_`var'_1, ///
				ycommon rows(1) imargin(2 2 0 0) ///
				title("by Initially Predominantly White", size(medium)) ///
				name(fig_s5_rate_10_`var'_combo, replace) ///
					note("Figure S5: Predominantly White vs Nonwhite")

		}


		* By locale
		foreach p in 0 1 2 {

			local v : label (igeotype) `p'
			estimates clear

			eststo igeotype_`p'_10: quietly reghdfe diff_oss_blwh2 ///
				$neighvars ///
				$schvars ///
				i.year##i.ngh_black_10 ///
				if $conditions & igeotype == `p', ///
				$regopts

			local i 1
			forvalues n = 0/2 {
				est restore igeotype_`p'_10
				quietly margins if ngh_black_10 == `n', ///
					dydx(year) noestimcheck post
				eststo m`i'
				local ++i
			}

			coefplot (m1 , label(Stable)     bcolor(navy)  ciopts(lcol(black))) ///
					 (m2 , label(Increasing) bcolor(green) ciopts(lcol(black))) ///
					 (m3 , label(Decreasing) bcolor(gray)  ciopts(lcol(black))) , ///
					 vert barwidth(0.2) ytitle("") recast(bar) ///
					 title("`v'", size(medium)) ///
					 xlabel(1 " " , noticks) ///
					 ysc(r(-10 10) off) ///
					 ylabel(-10(5)10, labsize(small)) yline(0) ///
					 xtitle("") ylabel(, labsize(small)) ///
					 color(*.5) $ciopts $figopts ///
					 mlabpos(10) mlabsize(medium) mlabcolor(black) ///
					 nokey ///
					 name(fig_rate_10_igeotype`p', replace)

		}

		graph combine fig_rate_10_igeotype0 ///
					  fig_rate_10_igeotype1 ///
					  fig_rate_10_igeotype2, ///
			ycommon rows(1) imargin(2 2 0 0) ///
			title("by locale", size(medium)) ///
			name(fig_s5_rate_10_locale_combo, replace) ///
			note("Figure S5: Locale")
			
			graph drop fig_rate_10*
			

			* Save Appendix Figures S5
			
				// Main	
			graph export "$figures\fig_s5_rate_10_main.tif", ///
					name("fig_s5_rate_10_main") ///
					as(tif) replace
			
				// Predominantly White	
			graph export "$figures\fig_s5_rate_10_predom_combo.tif", ///
					name("fig_s5_rate_10_predom_combo") ///
					as(tif) replace
			
				// Locale
			graph export "$figures\fig_s5_rate_10_locale_combo.tif", ///
				name("fig_s5_rate_10_locale_combo") ///
				as(tif) replace										